package com.jinbei.service.impl;

import com.jinbei.common.base.SocketBase;
import com.jinbei.common.socket.MessageWebSocket;
import com.jinbei.domain.Message;
import com.jinbei.mapper.MessageMapper;
import com.jinbei.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zflu
 * 消息service
 * Created by lucky on 2018/8/20.
 */
@Service
public class MessageServiceImpl implements MessageService{

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageWebSocket messageWebSocket;

    /**
     * @author zflu
     * @Description   socket消息新增
     * @date 2018/8/20 11:12
     * @param message
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int insertMessage(Message message , String uid) throws Exception{
        int result = messageMapper.insertMessage(message);
        //连接数量
        int onLine = messageWebSocket.countOnline();
        if(result > 0 && onLine > 0){
            String data = SocketBase.successWebSocket(String.valueOf(message.getId()), message.getType());
            //发送消息
            messageWebSocket.sendAllSocketMessage(data,uid);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   socket消息详情
     * @date 2018/8/20 11:13
     * @param id
     * @return com.jinbei.domain.Message
     */
    @Override
    public Message queryMessageById(int id)  throws Exception{
        return messageMapper.queryMessageById(id);
    }

    /**
     * @author zflu
     * @Description socket消息列表
     * @date 2018/8/20 11:14
     * @param
     * @return java.util.List<com.jinbei.domain.Message>
     */
    @Override
    public List<Message> queryMessageList() throws Exception {
        return messageMapper.queryMessageList();
    }
}
